package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignCell;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignCellGroup;

/**
 * @ClassName: DaoDcProjectDesignContent 
 * @Description:数据中心_项目_设计单元组维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectDesignCellGroup extends EntityRepositoryServiceImpl<DcProjectDesignCellGroup>{

	public DaoDcProjectDesignCellGroup(Dao dao) {
		super(dao);
	}
	
	//查询设计单元组
	public QueryResult findDesignCellGroups(NutMap param){
		List <NutMap> result_list = null;
		Pager pager = null;
		StringBuilder stringBuilder = new StringBuilder(" select dpdcg.design_cell_group_name , dpdcg.design_cell_group_code"
				+ " dpdcg.create_time , dpdcg.update_time , dpdp.project_design_phase_name , dpdm.project_design_major_name , "
				+ " dpdc.project_design_content_name "
				+ " from dc_project_design_cell_group dpdcg "
				+ " left join dc_project_design_phase dpdp on dpdcg.project_design_phase_id = dpdp.id and dpdp.del_flag = '0'"
				+ " left join dc_project_design_content dpdc on dpdcg.project_design_content_id = dpdc.id and dpdc.del_flag = '0'"
				+ " left join dc_project_design_major dpdm on dpdcg.project_design_major_id = dpdm.id and dpdm.del_flag = '0' "
				+ " where dpdcg.del_flag = '0' ");
		if(param.get("design_cell_group_name") != null){
			stringBuilder.append(" and dpdcg.design_cell_group_name like @design_cell_group_name ");
		}
		Sql sql = Sqls.create(stringBuilder.toString());
		if(param.get("design_cell_group_name") != null){
			sql.params().set("design_cell_group_name", "%"+param.getString("design_cell_group_name")+"%");
		}
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
			pager.setRecordCount(findDesignCellGroupsNum(param));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List <NutMap> list = new LinkedList<>();
				while (rs.next()){
					NutMap map = new NutMap();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						map.put(filed, rs.getString(filed));
					}
					list.add(map);
				}
				return list;
			}
		});
		dao().execute(sql);
		result_list = sql.getList(NutMap.class);
		return new QueryResult(result_list, pager);
	}
	
	
	//查询设计单元组数量
	public int findDesignCellGroupsNum(NutMap param){
		StringBuilder stringBuilder = new StringBuilder(" select count(dpdcg.id) num "
				+ " from dc_project_design_cell_group dpdcg "
				+ " left join dc_project_design_phase dpdp on dpdcg.project_design_phase_id = dpdp.id and dpdp.del_flag = '0'"
				+ " left join dc_project_design_content dpdc on dpdcg.project_design_content_id = dpdc.id and dpdc.del_flag = '0'"
				+ " left join dc_project_design_major dpdm on dpdcg.project_design_major_id = dpdm.id and dpdm.del_flag = '0' "
				+ " where dpdcg.del_flag = '0' ");
		if(param.get("design_cell_group_name") != null){
			stringBuilder.append(" and dpdcg.design_cell_group_name like @design_cell_group_name ");
		}
		Sql sql = Sqls.create(stringBuilder.toString());
		if(param.get("design_cell_group_name") != null){
			sql.params().set("design_cell_group_name", "%"+param.getString("design_cell_group_name")+"%");
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				int num = 0;
				while (rs.next()) {
					num = rs.getInt("num");
				}
				return num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}

}
